<? 
	include('base.php');
	User::protect();
	include_class('releases');
	include_class('m2');
	include_class('release_tracks');
	$rt = ReleaseTrack::get($_GET['id']);
	$doSubmit = false;
	if (!db::isError($rt)) {
		$r = $rt->getReleaseObject();
		if (!db::isError($r)) {
			if ($_POST['url']) {
				$doSubmit = true;
				$res = $rt->addAudioRemote($_POST['url']);
			} else if ($_POST['incoming_file']) {
				$doSubmit = true;
				$res = $rt->addAudioIncoming($_POST['incoming_file']);
			}
		}
	}
	$section='releases';
	if (!db::isError($res) && $doSubmit) {
		header('Location: release_track_edit.php?id=' . $_GET['id']);
	}
	
	$page_title = 'Add Track Audio';
	include('layout/header.php'); 

if (!db::isError($r) && !db::isError($rt)) { ?>

<div id="breadcrumb">
	<a href="index.php">Audition &#62;</a>
	<a href="releases.php">Releases &#62;</a>
	<a href="release_edit.php?id=<?=$r->getID()?>">View/Edit Release &#62;</a>
	<a href="release_track_edit.php?id=<?=$rt->getID()?>"><?=$rt->getTitle()?> &#62;</a>
	Add Audio File

</div>

<?
	if (db::isError($res)) {
		$res->outputList();
	}
	
	if (User::isAdmin()) {?>

		<h1>new audio track:</h1>
		<div class="inset">
		You can add an audio file to a track by typing in the URL of a remote file, or by adding a file that has already been uploaded to your web server's incoming directory.
		<br><br>
		
		<div class="edit-form">
		<form method="post" action="<?=$PHP_SELF?>?id=<?=$_GET['id']?>" id="add_remote_file" onsubmit="return validateRemote(extensions)">
			<h2>Add Remote File</h2>
			<input type="text" name="url" value="" style="width: 400px"><br>
			<input type="submit" name="remotefile" value="Add Remote File" class="button">
		</form>
		</div>
		<div class="edit-form">
		<?
			$incoming = Media::getIncomingFiles();
			if ($incoming) { ?>
		
		<form method="post" action="<?=$PHP_SELF?>?id=<?=$_GET['id']?>" id="add_incoming_file" onsubmit="return validateIncoming(extensions)">
			<h2>Add Incoming File</h2>
			<select name="incoming_file">
				<option value="">Select a file from your incoming directory</option>
				<? for ($i = 0; $i < count($incoming); $i++) {
					echo('<option value="' . $incoming[$i] . '">' . $incoming[$i] . '</option>');
				} ?>		
			</select>
			<input type="submit" name="incomingfile" value="Add Incoming File" class="button">
		</form>
		
		<? } else { ?>
			<h2>Add Incoming File</h2>
			There are no files in your incoming directory.<br>
		<? } ?>
		</div>
	<? } else {
		Error::outputDialog('Return to Release Track', 'release_track_edit.php?id=' . $_GET['id'],'You are not allowed to add audio to a track.');
	}

} else { ?>


<div id="breadcrumb">
	<a href="index.php">Audition &#62;</a>
	<a href="releases.php">Releases &#62;</a>
	Invalid Entry
</div>

<? if (db::isError($r)) {
	$r->outputDialog('Return to Releases','releases.php');
} else if (db::isError($rt)) {
	$rt->outputDialog('Return to Releases','releases.php');
}

}

include('layout/footer.php'); ?>
